package com.ld.common.controller;

import java.io.IOException;

import javax.servlet.ServletException;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ld.shieldsb.common.core.model.PropertiesModel;
import com.ld.shieldsb.common.core.util.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "基础功能过滤器")
@ConditionalOnProperty(prefix = "modules.sc.base", name = "enable", havingValue = "true", matchIfMissing = true)
@Controller
@RequestMapping("/common")
public class ProjectCommonController extends ProjectBasicController<Object> {

    @ApiOperation(value = "页面跳转", notes = "用于没有业务逻辑单纯跳转的页面")
    @GetMapping(path = "/to/**")
    public String adminSkip2Page() {
        setAttributes();
        String uri = request.getServletPath();
        uri = uri.replace("/common/to/", "/");
        log.warn(uri);
        return uri;
    }

    @ApiOperation(value = "图片上传回调", notes = "")
    @GetMapping(path = "uploadCallback")
    // 返回json格式
    @ResponseBody
    public Object uploadCallback() throws Exception {
        String result = getString("result");
        result = StringEscapeUtils.unescapeHtml(result); // 反转义，字符串可能被转义过
        JSONObject json_result = JSONObject.parseObject(result);
        if (result.indexOf("type") < 0) { // 兼容原有上传方法,原有方法不返回type
            return json_result;
        } else { // 只单图上传走这个
            String type = json_result.get("type").toString();
            if (type.indexOf(".") == 0) { // ueditor返回
                String imageHost = PropertiesModel.CONFIG.getString("uploadImageHost", "");
                json_result.put("url", imageHost + json_result.get("url").toString().replaceAll("http://(.*?)/", ""));
                // 附件保存至库
                if (StringUtils.isNotEmpty(json_result.getString("state")) && json_result.getString("state").equals("SUCCESS")) {
                    String name = json_result.getString("original");
                    String path = json_result.getString("url");
                    Long size = Long.parseLong(json_result.getString("size"));
//                    AffixModel affixModel = new AffixModel();
//                    affixModel.setName(name);
//                    affixModel.setPath(path);
//                    affixModel.setType(type);
//                    affixModel.setSize(size);
//                    affixModel.setAffixType(LdStaticParams.ATTFIX.ATTFIX_ONLINE);
//                    basicService.saveAffixModel(affixModel);
//                    json_result.put("affixId", affixModel.getId());
                }
                return json_result;
            } else {
                return json_result;
            }
        }
    }

    @ApiOperation(value = "检测系统是否正常运行", notes = "")
    @GetMapping(path = "ping")
    @ResponseBody
    public Object ping() throws ServletException, IOException {
        return "1";
    }

    @ApiOperation(value = "获取ip信息", notes = "")
    @GetMapping(path = "getIp")
    public Object getIp() throws ServletException, IOException {
        return "getIp";
    }

}
